{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 关联规则分析"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 定义规则，返回用户购买了的商品类型\n",
    "def acquire_goods(acquire_list):\n",
    "    goods_purchased = []\n",
    "    if acquire_list[0]>0:\n",
    "        goods_purchased.append(\"Food\")\n",
    "    if acquire_list[1]>0:\n",
    "        goods_purchased.append(\"Fresh\")\n",
    "    if acquire_list[2]>0:\n",
    "        goods_purchased.append(\"Drinks\")\n",
    "    if acquire_list[3]>0:\n",
    "        goods_purchased.append(\"Home\")\n",
    "    if acquire_list[4]>0:\n",
    "        goods_purchased.append(\"Beauty\")\n",
    "    if acquire_list[5]>0:\n",
    "        goods_purchased.append(\"Health\")\n",
    "    if acquire_list[6]>0:\n",
    "        goods_purchased.append(\"Baby\")\n",
    "    if acquire_list[7]>0:\n",
    "        goods_purchased.append(\"Pets\")\n",
    "    return goods_purchased"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_relevance = pd.read_csv(\"../data/order.csv\")\n",
    "# 删除不相关的列\n",
    "df_relevance.drop(['order','discount%','weekday','hour','total_items'],axis = 1,inplace=True)\n",
    "goods_list = df_relevance.iloc[:,1:].values.tolist()\n",
    "\n",
    "# 用来储存 客户购买的商品的列表\n",
    "acquire_goods_list = []\n",
    "for good in goods_list:\n",
    "    # 调用acquire_goods方法来返回客户购买的商品的列表\n",
    "    a = acquire_goods(good)\n",
    "    acquire_goods_list.append(a)\n",
    "    \n",
    "# 我们还可以实现将它添加到表格中\n",
    "df_relevance['购买的商品'] = acquire_goods_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>antecedents</th>\n",
       "      <th>consequents</th>\n",
       "      <th>antecedent support</th>\n",
       "      <th>consequent support</th>\n",
       "      <th>support</th>\n",
       "      <th>confidence</th>\n",
       "      <th>lift</th>\n",
       "      <th>leverage</th>\n",
       "      <th>conviction</th>\n",
       "      <th>zhangs_metric</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>(Drinks)</td>\n",
       "      <td>(Food)</td>\n",
       "      <td>0.830767</td>\n",
       "      <td>0.835133</td>\n",
       "      <td>0.743400</td>\n",
       "      <td>0.894836</td>\n",
       "      <td>1.071489</td>\n",
       "      <td>0.049599</td>\n",
       "      <td>1.567712</td>\n",
       "      <td>0.394244</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>(Food)</td>\n",
       "      <td>(Drinks)</td>\n",
       "      <td>0.835133</td>\n",
       "      <td>0.830767</td>\n",
       "      <td>0.743400</td>\n",
       "      <td>0.890157</td>\n",
       "      <td>1.071489</td>\n",
       "      <td>0.049599</td>\n",
       "      <td>1.540687</td>\n",
       "      <td>0.404686</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>(Fresh)</td>\n",
       "      <td>(Food)</td>\n",
       "      <td>0.612167</td>\n",
       "      <td>0.835133</td>\n",
       "      <td>0.580200</td>\n",
       "      <td>0.947781</td>\n",
       "      <td>1.134886</td>\n",
       "      <td>0.068959</td>\n",
       "      <td>3.157222</td>\n",
       "      <td>0.306457</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>(Fresh)</td>\n",
       "      <td>(Drinks)</td>\n",
       "      <td>0.612167</td>\n",
       "      <td>0.830767</td>\n",
       "      <td>0.561500</td>\n",
       "      <td>0.917234</td>\n",
       "      <td>1.104081</td>\n",
       "      <td>0.052932</td>\n",
       "      <td>2.044717</td>\n",
       "      <td>0.243067</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>(Drinks, Food)</td>\n",
       "      <td>(Fresh)</td>\n",
       "      <td>0.743400</td>\n",
       "      <td>0.612167</td>\n",
       "      <td>0.538333</td>\n",
       "      <td>0.724150</td>\n",
       "      <td>1.182930</td>\n",
       "      <td>0.083249</td>\n",
       "      <td>1.405959</td>\n",
       "      <td>0.602656</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>(Drinks, Fresh)</td>\n",
       "      <td>(Food)</td>\n",
       "      <td>0.561500</td>\n",
       "      <td>0.835133</td>\n",
       "      <td>0.538333</td>\n",
       "      <td>0.958741</td>\n",
       "      <td>1.148010</td>\n",
       "      <td>0.069406</td>\n",
       "      <td>3.995941</td>\n",
       "      <td>0.294019</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>(Food, Fresh)</td>\n",
       "      <td>(Drinks)</td>\n",
       "      <td>0.580200</td>\n",
       "      <td>0.830767</td>\n",
       "      <td>0.538333</td>\n",
       "      <td>0.927841</td>\n",
       "      <td>1.116849</td>\n",
       "      <td>0.056323</td>\n",
       "      <td>2.345283</td>\n",
       "      <td>0.249223</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>(Fresh)</td>\n",
       "      <td>(Drinks, Food)</td>\n",
       "      <td>0.612167</td>\n",
       "      <td>0.743400</td>\n",
       "      <td>0.538333</td>\n",
       "      <td>0.879390</td>\n",
       "      <td>1.182930</td>\n",
       "      <td>0.083249</td>\n",
       "      <td>2.127521</td>\n",
       "      <td>0.398732</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>(Drinks)</td>\n",
       "      <td>(Home)</td>\n",
       "      <td>0.830767</td>\n",
       "      <td>0.677133</td>\n",
       "      <td>0.615933</td>\n",
       "      <td>0.741404</td>\n",
       "      <td>1.094915</td>\n",
       "      <td>0.053394</td>\n",
       "      <td>1.248535</td>\n",
       "      <td>0.512235</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>(Home)</td>\n",
       "      <td>(Drinks)</td>\n",
       "      <td>0.677133</td>\n",
       "      <td>0.830767</td>\n",
       "      <td>0.615933</td>\n",
       "      <td>0.909619</td>\n",
       "      <td>1.094915</td>\n",
       "      <td>0.053394</td>\n",
       "      <td>1.872443</td>\n",
       "      <td>0.268492</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>(Food)</td>\n",
       "      <td>(Home)</td>\n",
       "      <td>0.835133</td>\n",
       "      <td>0.677133</td>\n",
       "      <td>0.611333</td>\n",
       "      <td>0.732019</td>\n",
       "      <td>1.081056</td>\n",
       "      <td>0.045837</td>\n",
       "      <td>1.204811</td>\n",
       "      <td>0.454781</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>(Home)</td>\n",
       "      <td>(Food)</td>\n",
       "      <td>0.677133</td>\n",
       "      <td>0.835133</td>\n",
       "      <td>0.611333</td>\n",
       "      <td>0.902826</td>\n",
       "      <td>1.081056</td>\n",
       "      <td>0.045837</td>\n",
       "      <td>1.696607</td>\n",
       "      <td>0.232227</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>(Drinks, Food)</td>\n",
       "      <td>(Home)</td>\n",
       "      <td>0.743400</td>\n",
       "      <td>0.677133</td>\n",
       "      <td>0.570500</td>\n",
       "      <td>0.767420</td>\n",
       "      <td>1.133337</td>\n",
       "      <td>0.067119</td>\n",
       "      <td>1.388196</td>\n",
       "      <td>0.458494</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>(Drinks, Home)</td>\n",
       "      <td>(Food)</td>\n",
       "      <td>0.615933</td>\n",
       "      <td>0.835133</td>\n",
       "      <td>0.570500</td>\n",
       "      <td>0.926237</td>\n",
       "      <td>1.109088</td>\n",
       "      <td>0.056114</td>\n",
       "      <td>2.235074</td>\n",
       "      <td>0.256098</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>(Food, Home)</td>\n",
       "      <td>(Drinks)</td>\n",
       "      <td>0.611333</td>\n",
       "      <td>0.830767</td>\n",
       "      <td>0.570500</td>\n",
       "      <td>0.933206</td>\n",
       "      <td>1.123307</td>\n",
       "      <td>0.062625</td>\n",
       "      <td>2.533665</td>\n",
       "      <td>0.282431</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>(Home)</td>\n",
       "      <td>(Drinks, Food)</td>\n",
       "      <td>0.677133</td>\n",
       "      <td>0.743400</td>\n",
       "      <td>0.570500</td>\n",
       "      <td>0.842522</td>\n",
       "      <td>1.133337</td>\n",
       "      <td>0.067119</td>\n",
       "      <td>1.629438</td>\n",
       "      <td>0.364391</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        antecedents     consequents  antecedent support  consequent support  \\\n",
       "0          (Drinks)          (Food)            0.830767            0.835133   \n",
       "1            (Food)        (Drinks)            0.835133            0.830767   \n",
       "2           (Fresh)          (Food)            0.612167            0.835133   \n",
       "3           (Fresh)        (Drinks)            0.612167            0.830767   \n",
       "4    (Drinks, Food)         (Fresh)            0.743400            0.612167   \n",
       "5   (Drinks, Fresh)          (Food)            0.561500            0.835133   \n",
       "6     (Food, Fresh)        (Drinks)            0.580200            0.830767   \n",
       "7           (Fresh)  (Drinks, Food)            0.612167            0.743400   \n",
       "8          (Drinks)          (Home)            0.830767            0.677133   \n",
       "9            (Home)        (Drinks)            0.677133            0.830767   \n",
       "10           (Food)          (Home)            0.835133            0.677133   \n",
       "11           (Home)          (Food)            0.677133            0.835133   \n",
       "12   (Drinks, Food)          (Home)            0.743400            0.677133   \n",
       "13   (Drinks, Home)          (Food)            0.615933            0.835133   \n",
       "14     (Food, Home)        (Drinks)            0.611333            0.830767   \n",
       "15           (Home)  (Drinks, Food)            0.677133            0.743400   \n",
       "\n",
       "     support  confidence      lift  leverage  conviction  zhangs_metric  \n",
       "0   0.743400    0.894836  1.071489  0.049599    1.567712       0.394244  \n",
       "1   0.743400    0.890157  1.071489  0.049599    1.540687       0.404686  \n",
       "2   0.580200    0.947781  1.134886  0.068959    3.157222       0.306457  \n",
       "3   0.561500    0.917234  1.104081  0.052932    2.044717       0.243067  \n",
       "4   0.538333    0.724150  1.182930  0.083249    1.405959       0.602656  \n",
       "5   0.538333    0.958741  1.148010  0.069406    3.995941       0.294019  \n",
       "6   0.538333    0.927841  1.116849  0.056323    2.345283       0.249223  \n",
       "7   0.538333    0.879390  1.182930  0.083249    2.127521       0.398732  \n",
       "8   0.615933    0.741404  1.094915  0.053394    1.248535       0.512235  \n",
       "9   0.615933    0.909619  1.094915  0.053394    1.872443       0.268492  \n",
       "10  0.611333    0.732019  1.081056  0.045837    1.204811       0.454781  \n",
       "11  0.611333    0.902826  1.081056  0.045837    1.696607       0.232227  \n",
       "12  0.570500    0.767420  1.133337  0.067119    1.388196       0.458494  \n",
       "13  0.570500    0.926237  1.109088  0.056114    2.235074       0.256098  \n",
       "14  0.570500    0.933206  1.123307  0.062625    2.533665       0.282431  \n",
       "15  0.570500    0.842522  1.133337  0.067119    1.629438       0.364391  "
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 导入所需库\n",
    "from mlxtend.preprocessing import TransactionEncoder\n",
    "from mlxtend.frequent_patterns import fpgrowth\n",
    "from mlxtend.frequent_patterns import association_rules\n",
    "# 将样本数据转换为适合fp-growth算法的格式\n",
    "te = TransactionEncoder()\n",
    "te_ary = te.fit(acquire_goods_list).transform(acquire_goods_list)\n",
    "df = pd.DataFrame(te_ary, columns=te.columns_)\n",
    "\n",
    "# 使用fp-growth算法挖掘频繁项集\n",
    "frequent_itemsets = fpgrowth(df, min_support=0.5, use_colnames=True)\n",
    "\n",
    "# 关联规则生成，设置最小置信度阈值为0.7\n",
    "rules = association_rules(frequent_itemsets, metric=\"confidence\", min_threshold=0.7)\n",
    "\n",
    "rules"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.9.13 ('base')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "79c2bc36f36ebca92fe5f3c73355a3866f3a34a4fd0b1d43087d5313dd5fd5ff"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
